package ui;

import pojo.FileItem;
import utils.NetworkUtils;
import utils.NetworkUtilsV2;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.event.*;
import java.io.IOException;
import java.util.List;

public class ClientForm {
    private JTable tbfile;
    private JButton btUpload;
    private JButton btDownLoad;
    private JButton btDelete;
    private JButton btRefresh;
    public JPanel panel1;
    //文件ID
    public  Long fileId;
    //用户ID
    private Long userId;

    public ClientForm(Long userId)  {
        //从登陆界面把用户ID传入
        this.userId=userId;
            refreshFileList();
            //添加刷新按钮监听
        btRefresh.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                refreshFileList();
            }
        });
        //添加上传按钮监听
        btUpload.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String path = JOptionPane.showInputDialog("请输入上传文件路径");
                if(path.isEmpty()){
                    JOptionPane.showMessageDialog(null,"请输入路径");
                    return;
                }
                NetworkUtilsV2.uploadFile(userId,path);
                JOptionPane.showMessageDialog(null,"上传完毕");
            }
        });
        //下载按钮的监听器
        btDownLoad.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(fileId ==null ){
                    JOptionPane.showMessageDialog(null,"请选择下载的文件");
                    return;
                }
                NetworkUtilsV2.downloadFile(fileId );
                JOptionPane.showMessageDialog(null,"下载完毕");
                fileId = null;

            }
        });
        tbfile.addComponentListener(new ComponentAdapter() {
        });

        //表格鼠标点击事件
        tbfile.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                //获取文件中ID
                int row = tbfile.getSelectedRow();
                fileId=Long.valueOf(tbfile.getModel().getValueAt(row,0).toString());
            }
        });

        //删除文件监听器
        btDelete.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(fileId  ==null ){
                    JOptionPane.showMessageDialog(null,"请选择删除的文件");
                    return;
                }
                NetworkUtilsV2.deleteFiles(fileId );
                JOptionPane.showMessageDialog(null,"删除成功");
                fileId = null;



            }
        });
    }


    public void refreshFileList(){
        //连接服务器获得文件列表
        List<FileItem> fileItems = NetworkUtilsV2.getFileItems(userId);
        //将文件集合转换为二维数组
        Object[][] data=new Object[fileItems.size()][3];
        for (int i = 0; i <data.length ; i++) {
            FileItem fileItem = fileItems.get(i);
            data[i][0]=fileItem.getId();
            data[i][1]=fileItem.getFileName();
            data[i][2]=fileItem.getLength();
        }
        //将数据显示在表格中
        Object[] columnNames={"编号","文件名","长度"};
        tbfile.setModel(new DefaultTableModel(data,columnNames));
    }
//
//    public static void main(String[] args){
//        JFrame frame = new JFrame("ClientForm");
//        frame.setContentPane(new ClientForm().panel1);
//        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        frame.pack();
//        frame.setSize(800,500);
//        frame.setVisible(true);
//    }
}
